package com.coke.elastic.search.utils;

import cn.hutool.core.io.FileUtil;

import java.io.File;
import java.util.List;
import java.util.Optional;
public class FileUtils {

       public static Boolean reFileName(String path,String name,String repalceName){
        try {
            if (!FileUtil.exist(path)){
                return Boolean.TRUE;
            }
            File[] ls = FileUtil.ls(path);
            List<File> files = Arrays.asList(ls);
            Optional.of(files)
                    .map(List::stream)
                    .orElseThrow(() -> new RuntimeException("修改文件出错"))
                    .forEach(item ->{
                        String fileName = item.getName();
                        if (fileName.contains(name)){
                            fileName= fileName.replace(name,repalceName);
                            FileUtil.rename(item,fileName,false,true);
                        }
                        if (FileUtil.isDirectory(item)){
                            reFileName(item.getPath(),name,repalceName);
                        }
                    });
            return Boolean.TRUE;
        }catch (Exception e){
            e.printStackTrace();
            return Boolean.FALSE;
        }
    }

    //
    public static void main(String[] args) {
        reFileName("E:\\博学谷 - 2019JavaEE在线就业班2.0","【 666java.com】","");
    }
}
